$(function() {
	$(".context").height($(window).height() - 70);
	$("#grid").simplegrid({
		onClick: function(element, role) {
			$("#menu-ul").tree({
				url: "system/Role/menu?roleid=" + role.id
			});
//			$("#button-ul").tree({
//				url : "system/Button/getRole?roleid= " + role.id
//			});
		}
	});
});

/**
 * 添加
 */
function btn_add() {
	$("#role-button").unbind();
	$("#role-button").click(exec_add);
	$("#role-div").window({title: '添加', iconCls:'icon-add', closed: false});
}

function exec_add() {
	var data = $("#role-form").serializeArray();
	$.ajax({
		url: 'system/Role/add',
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(data) {
			if(data.result > 0) {
				alert("添加成功");
				$("#grid").simplegrid("refresh");
				$('#role-form')[0].reset();$('#role-div').window('close');
			} else {
				this.error();
			}
		},
		error: function() {
			alert("添加失败");
		}
	});
}

/** 编辑 */
function btn_edit() {
	var role = $("#grid").simplegrid("getSelected");
	if (role.length == 0) {
		alert("没有选择记录.");
		return;
	}
	role = role[0];
	$("#role-button").unbind();
	$("#role-button").click(exec_edit);
	$("#role-form").form("load", {
		'roleInfo.id': role.id,
		'roleInfo.name': role.name
	});
	$("#role-div").window({title: '编辑', iconCls:'icon-edit', closed: false});
}

function exec_edit() {
	var data = $("#role-form").serializeArray();
	$.ajax({
		url: 'system/Role/edit',
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(data) {
			if(data.result > 0) {
				alert("编辑成功");
				$("#grid").simplegrid("refresh");
				$('#role-form')[0].reset();$('#role-div').window('close');
			} else {
				this.error();
			}
		},
		error: function() {
			alert("编辑失败");
		}
	});
}

/** 删除 */
function btn_delete() {
	var role = $("#grid").simplegrid("getSelected");
	if (role.length == 0) {
		alert("没有选择记录.");
		return;
	}
	role = role[0];
	if (confirm("确认删除[" + role.name + "]吗?")) {
		$.ajax({
			url: 'system/Role/del',
			type: 'post',
			data: {id: role.id},
			success: function() {
				alert("删除成功");
				$("#grid").simplegrid("refresh");
			},
			error: function() {
				alert("删除失败");
			}
		});
	}
}

/**
 * 菜单设置
 */
function exec_menu() {
	var role = $("#grid").simplegrid("getSelected");
	role = role[0];
	var menu = $("#menu-ul").tree("getChecked");
	var param = new Array();
	param.push({name: 'exec', value: 'set'});
	param.push({name: 'roleid', value: role.id});
	for ( var i = 0; i < menu.length; i++) {
		if ($('#menu-ul').tree('isLeaf', menu[i].target)) {
			param.push({name: 'menuid', value: menu[i].id});
		}
	}
	$.ajax({
		url: 'system/Role/menu',
		type: 'post',
		dataType: 'json',
		data: param,
		success: function(data) {
			if (data.result > 0) {
				alert("保存成功");
			} else {
				this.error();
			}
		},
		error: function(e) {
			alert("保存失败:" + e);
		}
	});
}

function exec_button(){
	var role = $("#grid").simplegrid("getSelected");
	role = role[0];
	var menu = $("#button-ul").tree("getChecked");
	var param = new Array();
	param.push({name: 'exec', value: 'set'});
	param.push({name: 'roleid', value: role.id});
	for ( var i = 0; i < menu.length; i++) {
		if ($('#button-ul').tree('isLeaf', menu[i].target)) {
			param.push({name: 'btnid', value: menu[i].id});
		}
	}
	$.ajax({
		url: 'system/Button/setRole',
		type: 'post',
		dataType: 'json',
		data: param,
		success: function(data) {
			if (data.result > 0) {
				alert("保存成功");
			} else {
				this.error();
			}
		},
		error: function(e) {
			alert("保存失败:" + e);
		}
	});
}
